package com.yiqi.news.mvp.presenter

import android.app.Application

import com.jess.arms.integration.AppManager
import com.jess.arms.di.scope.ActivityScope
import com.jess.arms.mvp.BasePresenter
import com.jess.arms.http.imageloader.ImageLoader
import com.yiqi.huize.isNull
import com.yiqi.news.app.utils.RxUtils
import com.yiqi.news.app.utils.ValidatorUtil
import com.yiqi.news.entity.BankCardVO
import com.yiqi.news.entity.BankTypeVO
import com.yiqi.news.entity.BaseResponse
import com.yiqi.news.entity.User
import me.jessyan.rxerrorhandler.core.RxErrorHandler
import javax.inject.Inject

import com.yiqi.news.mvp.contract.BankcardContract
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 06/18/2019 17:36
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
class BankcardPresenter
@Inject
constructor(model: BankcardContract.Model, rootView: BankcardContract.View) :
        BasePresenter<BankcardContract.Model, BankcardContract.View>(model, rootView) {
    @Inject
    lateinit var mErrorHandler: RxErrorHandler
    @Inject
    lateinit var mApplication: Application
    @Inject
    lateinit var mImageLoader: ImageLoader
    @Inject
    lateinit var mAppManager: AppManager


    override fun onDestroy() {
        super.onDestroy();
    }


    fun doAddCard(accBankName: BankTypeVO, accNo: String, accType: Int, accUser: String, identityCard: String) {


        if (!ValidatorUtil.isIDCard(identityCard)) {
            mRootView.showAddCardError("身份证格式有误")
            return
        }

        if (accBankName.isNull()){
            mRootView.showAddCardError("请选择银行卡类型")
            return
        }

        if (!ValidatorUtil.checkBankCard(accNo)) {
            mRootView.showAddCardError("请输入正确的银行卡号")
            return
        }
        var map = HashMap<String, Any?>()
        map["accBankId"] = accBankName.id
        map["accNo"] = accNo
        map["accType"] = accType
        map["accUser"] = accUser
        map["identityCard"] = identityCard
        mModel.doAddCard(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object : ErrorHandleSubscriber<BaseResponse<User>>(mErrorHandler) {

                    override fun onNext(t: BaseResponse<User>) {
                        if (t.isSuccess) {
                            mRootView.showAddCardSuccess()
                        } else {
                            mRootView.showAddCardError(t.msg)
                        }
                    }


                })
    }

    fun doGetBanklist() {
        mModel.doCardType()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object : ErrorHandleSubscriber<BaseResponse<List<BankTypeVO>>>(mErrorHandler) {

                    override fun onNext(t: BaseResponse<List<BankTypeVO>>) {
                        if (t.isSuccess) {
                            mRootView.showCardTypeSuccess(t.data)
                        } else {
                            mRootView.showAddCardError(t.msg)
                        }

                    }

                })

    }

}
